'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var battleRecordModel = require('../../../../models/battleRecordModel');
var atkData = require('../../../../libs/war/atkData');
var battleData = require('../../../../libs/war/battleData');
var onlineService = require('../../../../services/onlineService');
var taskService = require('../../../../services/taskService');
var battleService = require('../../../../services/battleService');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var rivalService = require('../../../../services/rivalService');
var pvpGuideData = require('../../../../libs/war/pvpGuideData');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var pvp_guide_id = msg.pvp_guide_id;
    if (!pvp_guide_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var atk_data;
    var def_data;
    async.waterfall(
        [
            function (cb) {
                // 被攻击方数据
                def_data = _getDef(pvp_guide_id);
                if (!def_data) {
                    return next(null, {
                        code: Code.BATTLE_DEF_NOT_EXIST
                    });
                }

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (data, cb) {
                atk_data = data;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    record_id: 0,
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (pvp_guide_id) {
    return pvpGuideData.getData(pvp_guide_id);
};
